package com.yipao.wx.web.user;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.yipao.core.util.ResponseUtil;
import com.yipao.db.domain.user.User;
import com.yipao.db.service.devinfo.DevInfoService;
import com.yipao.db.service.order.CartService;
import com.yipao.db.service.order.OrderService;
import com.yipao.db.service.user.UserService;
import com.yipao.wx.annotation.LoginUser;
import com.yipao.wx.service.UserInfoService;
import io.swagger.models.auth.In;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户服务
 *
 * @author tianhao
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;
    @Resource
    private DevInfoService devInfoService;
    @Resource
    private CartService cartService;

    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("index")
    public Object list(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unLogin();
        }

        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("order", orderService.orderInfo(userId));
        return ResponseUtil.ok(data);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("info")
    public Object getUserInfo(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unLogin();
        }
        User user = userService.findById(userId);
        user.setPassword(null);
        return ResponseUtil.ok(user);
    }

    /**
     * 用户绑定设备
     *
     * @param userId
     * @return
     */
    @PostMapping("bindDevId")
    public Object bindDevId(@LoginUser Integer userId, @RequestBody JSONObject jsonObject) {
        if (userId == null) {
            return ResponseUtil.unLogin();
        }
        String imei = jsonObject.getString("imei");
        Integer devId;
        if (!StringUtils.isEmpty(imei)) {
            // 根据imei查询设备id
            devId = devInfoService.getIdByImei(imei);
        } else {
            devId = jsonObject.getInteger("devId");
        }
        if (devId == null) {
            return ResponseUtil.fail(ResponseUtil.STATUS_CODE_BAD_ARGUMENT, "设备不存在");
        }
        // 查询修改之前的设备id,比较是否修改了设备
        Integer oldDevId = userService.findDevIdById(userId);
        if (StringUtils.isEmpty(oldDevId) || !devId.equals(oldDevId)) {
            // 修改用户的设备id
            int i = userService.updateUserDevIdById(userId, devId);
            if (i > 0) {
                // 清空用户的购物车
                cartService.clearGoods(userId, null, null);
                // 根据设备id,返回设备信息
                return ResponseUtil.ok(devInfoService.selectByPrimaryKey(devId));
            }
            return ResponseUtil.fail();
        }
        // 设备id和之前绑定的一致,无需修改
        return ResponseUtil.ok(devInfoService.selectByPrimaryKey(devId));

    }

}

